package cn.jasonone.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CharsetFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		HttpServletRequest req = (HttpServletRequest) request;
		String accept = req.getHeader("Accept");
		if(accept.contains("application/json")) {
			response.setContentType("application/json;charset=UTF-8");
		}
		else if(accept.contains("application/javascript")) {
			response.setContentType("application/javascript;charset=UTF-8");
		}
		else if(accept.contains("text/css")) {
			response.setContentType("text/css;charset=UTF-8");
		}
		else if(accept.contains("text/html")) {
			response.setContentType("text/html;charset=UTF-8");
		}
		// 请求(进入)
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}
	
	

}
